﻿
namespace Mulala.Cavan.Presentation.Mvc.ModelBinder
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Spring.Context;
    using Spring.Context.Support;
    using Mulala.Cavan.Application.Seedwork;

    public class CorporationSearchBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            IApplicationContext springContext = ContextRegistry.GetContext();
            IAreaAppService areaAppService = springContext.GetObject(MulalaAppServiceConst.AREAAPPSERVICE) as IAreaAppService;

            string spell = string.Empty;
            string conditions = string.Empty;
            int currentPage = 1;
            if (bindingContext.ValueProvider.GetValue("area").IsNotNull())
            {
                spell = bindingContext.ValueProvider.GetValue("area").AttemptedValue;
            }
            if (bindingContext.ValueProvider.GetValue("currentPage").IsNotNull())
            {
                int.TryParse(bindingContext.ValueProvider.GetValue("currentPage").AttemptedValue, out currentPage);
            }
            if (bindingContext.ValueProvider.GetValue("conditions").IsNotNull())
            {
                conditions = bindingContext.ValueProvider.GetValue("conditions").AttemptedValue;
            }

            if (spell.IsEmpty())
            {
                spell = Project.Settings.DefaultArea.Spell;
            }
            string areaCode = areaAppService.GetSingleBySearch(AreaSearchFactory.CreateAreaSearchBySpell(spell)).ID;
            CorporationIdentifier.CorporationType? type = null;
            int experience = 0;
            int scale = 0;
            List<int> skillPositions = new List<int>();
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();

            if (conditions.IsNotEmpty())
            {
                int value = 0;
                string[] conditionArray = conditions.Split('-');
                if (conditionArray.Length > 0)
                {
                    foreach (string condition in conditionArray)
                    {
                        if (condition.Contains('t'))
                        {
                            int.TryParse(condition.Substring(1), out value);
                            type = (CorporationIdentifier.CorporationType)value;
                        }
                        if (condition.Contains('e'))
                        {
                            int.TryParse(condition.Substring(1), out experience);
                        }
                        if (condition.Contains("sl"))
                        {
                            int.TryParse(condition.Substring(2), out scale);
                        }
                        if (condition.Contains("sp"))
                        {
                            int.TryParse(condition.Substring(2), out value);
                            skillPositions.Add(value);
                        }
                        if (condition.Contains("ob"))
                        {
                            int.TryParse(condition.Substring(2), out value);
                            if (value == 1)
                            {
                                orderBy.Add("ViewTotal", true);
                            }
                            else if (value == 2)
                            {
                                orderBy.Add("ViewTotal", false);
                            }
                            else if (value == 3)
                            {
                                orderBy.Add("BusinessExperience", true);
                            }
                            else if (value == 4)
                            {
                                orderBy.Add("BusinessExperience", false);
                            }
                        }
                    }
                }
            }

            orderBy.Add("CorporationStatus.RefreshTime", true);
            orderBy.Add("CorporationStatus.VIPTimeout", true);

            CorporationSearch corporationSearch = CorporationSearchFactory.CreateCorporationSearch(type, experience, areaCode, scale, skillPositions.ToArray(), orderBy, currentPage, Project.Settings.MaxRowsPerPage);
            corporationSearch.AreaSpell = spell;

            return corporationSearch;
        }
    }
}